package main

import "fmt"

func main() {
	//assertDemoOne()
	assertDemoTwo()
}

func assertDemoTwo() {
	computer := &Computer{}
	var usbArrs [3]USB
	usbArrs[0] = &Mouse{"极光鼠标"}
	usbArrs[1] = &Camera{"太阳照相机"}
	usbArrs[2] = &Mouse{"南极鼠标"}
	for i := 0; i < 3; i++ {
		computer.Run(usbArrs[i])
	}
}

type USB interface {
	start()
	stop()
}

//定义实现
type Mouse struct {
	name string
}

type Camera struct {
	name string
}

//实现接口的方法
func (m *Mouse) start() {
	fmt.Println(m.name, " Mouse started...")
}

func (c *Camera) start() {
	fmt.Println(c.name, " Camera started...")
}

func (m *Mouse) stop() {
	fmt.Println(m.name, " Mouse stoped...")
}

func (m *Camera) stop() {
	fmt.Println(m.name, " Canera stoped...")
}

func (c *Camera) snapshot() {
	fmt.Println(c.name, "Camera snapshot started...")
}

type Computer struct {
}

func (computer *Computer) Run(usb USB) {
	usb.start()
	if obj, ok := usb.(*Camera); ok {
		obj.snapshot()
	}
	usb.stop()
}

func assertDemoOne() {
	var x interface{}
	var b2 float32 = 1.1
	x = b2
	y := x.(int) //如果类型不匹配，就会报panic，因此类型断言时，要确保原来的空接口指向的就是断言的类型
	if y, ok := x.(float32); ok {
		fmt.Println("convert success")
		fmt.Printf("y的类型是%T值是=%v", y, y)
	} else {
		fmt.Println("convert fail")
	}
	fmt.Printf("y的类型是 %T 值是=%v", y, y)
}
